package cate.game.role.res.forge;

import cate.common.table.d.GDEquip;
import cate.common.table.d.GDFunc;
import cate.common.util.GameResult;
import cate.game.role.FuncOpenChecker;
import cate.game.role.Role;
import cate.game.role.res.forge.msg.RoleForgeInfoResp;
import easy.java.dev.note.NoteField;

import java.util.HashMap;
import java.util.Map;


public class RoleForge extends FuncOpenChecker {
    @NoteField("key:装备部位，value:设置值")
    public Map<Byte, ForgePo> forgePoMap;

    @Override
    public void initialize(Role role) {
        super.initialize(role);
        if (forgePoMap == null) {
            forgePoMap = new HashMap<>();
        }
    }

    public GameResult<RoleForge> forgeConfig(byte pos, int quality, int star) {
        GameResult<RoleForge> r = new GameResult<>();
        r.data = this;
        if (!GDEquip.checkPos(pos)) {
            return r.fail("位置不存在");
        }
        ForgePo f = new ForgePo();
        f.quality = quality;
        f.star = star;
        forgePoMap.put(pos, f);
        return r;
    }

    public boolean canForge(byte pos, int quality, int star) {
        ForgePo po = forgePoMap.get(pos);
        if (po == null) {
            return true;
        } else {
            if (po.star == 0) {
                return true;
            }else {
                return quality < po.quality || (po.quality == quality && po.star >= star);
            }
        }
    }

    public void noticeUpdate() {
        if (!forgePoMap.isEmpty()) {
            role.sendNow(new RoleForgeInfoResp(this));
        }
    }

    @Override
    public int getFuncId() {
        return GDFunc.GOD_EQUIP;
    }

    @Override
    public void onAfterOpen() {

    }

}
